package com.example.controller;

import com.example.common.Code;
import com.example.common.Result;
import com.example.domain.Salary;
import com.example.service.SalaryService;
import com.example.utils.ExcelUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luo
 * 薪资管理
 */
@RestController
@RequestMapping("/salarys")
public class SalaryController {

    @Autowired
    private SalaryService salaryService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping
    public Result selectSalary(){
        List<Salary> salaries = salaryService.selectSalary();
        Integer code = salaries != null ? Code.GET_OK : Code.GET_ERR;
        String msg = salaries != null ? "" : "数据查询失败，请重试！";
        return new Result(code, salaries, msg);
    }
    @GetMapping("{id}")
    public Result selectSalaryById(@PathVariable Integer id){
        Salary salarys = null;

        String key = "Salary_ID：" +id;
        //先从redis中获取缓存数据
        salarys = (Salary) redisTemplate.opsForValue().get(key);

        if(salarys != null){
            //如果存在，直接返回，无需查询数据库
            return new Result(Code.GET_OK,salarys,"查询成功！");
        }
        salarys = salaryService.selectSalaryById(id);
        redisTemplate.opsForValue().set(key,salarys,60, TimeUnit.MINUTES);

        String msg = salarys!=null ? "" : "数据查询失败，请重试！";
        Integer code = salarys != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,salarys,msg);
    }
    @GetMapping("/ByName/{name}")
    public Result selectSalaryByName(@PathVariable String name){
        List<Salary> salaries = salaryService.selectSalaryByName(name);
        String msg = salaries!= null ? "" : "数据查询失败，请重试！";
        Integer code = salaries != null ? Code.GET_OK : Code.GET_ERR;
        return new Result(code,salaries,msg);
    }
    @PostMapping
    public Result insertSalary(@RequestBody Salary salary) {
        int i = salaryService.addSalary(salary);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";
        if (i > 0) {
            System.out.println("更新成功！");
            return new Result(Code.INSERT_OK, i, msg);
        } else {
            System.out.println("更新失败！");
            return new Result(Code.INSERT_ERR, i, msg);
        }
    }
    @PutMapping
    public Result updateSalary(@RequestBody Salary salary) {
        int i = salaryService.updateSalary(salary);
        String msg = i > 0 ? "" : "数据更新失败，请重试！";

        //删除redis中缓存
        String key = "Salary_ID：" +salary.getId();
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("更新成功！");
            return new Result(Code.UPDATE_OK, i, msg);
        } else {
            System.out.println("更新失败！");
            return new Result(Code.UPDATE_ERR, i, msg);
        }
    }
    @DeleteMapping("/{id}")
    public Result deleteSalary(@PathVariable Integer id) {
        int i = salaryService.deleteSalary(id);
        String msg = i > 0 ? "" : "数据设置失败，请重试！";

        //删除redis中缓存
        String key = "Salary_ID：" +id;
        redisTemplate.delete(key);

        if (i > 0) {
            System.out.println("删除成功！");
            return new Result(Code.DELETE_OK, i, msg);
        } else {
            System.out.println("删除失败！");
            return new Result(Code.DELETE_ERR, i, msg);
        }
    }
    @GetMapping("/{page}/{size}")
    public Result findUserList(@PathVariable int page,@PathVariable int size) {
        PageInfo<Salary> page1 = salaryService.findPage(page, size);
        Integer code = page1 != null ? Code.GET_OK : Code.GET_ERR;
        String msg = page1 != null ? "" : "分页失败，请重试！";
        return new Result(code, page1, msg);
    }
    @RequestMapping("/makeExcel")
    public void makeExcel(HttpServletResponse response) throws Exception {
        //打印Excel表
        ExcelUtils.exportExcel(response, "salary.xlsx", salaryService.getOutputExcelProjectData());
    }
}
